/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* Order.java
*
* Created on November 27, 2007, 9:39 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
 */
package genomemap.data;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * wrapper class for an order. This is to help in JXM persistence.
 * @author stewari1
 */
@Entity
public class OrderElement implements Comparable<OrderElement>, Serializable {

    /** Field description */
    private static final long serialVersionUID = 42L;

    /**
     * the integer order value wrapped by this class
     */
    private int val = -1;

    /** Field description */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    /** Creates a new instance of Order */
    public OrderElement() {}

    /**
     * Constructs ...
     *
     * @param val description
     */
    public OrderElement(int val) {
        this.val = val;
    }

    public int getVal() {
        return val;
    }

    @Override
    public int hashCode() {
        return new Integer(val).hashCode();
    }

    @Override
    public boolean equals(Object obj) {

        if (obj instanceof OrderElement) {

            OrderElement order = (OrderElement) obj;

            return order.getVal() == getVal();
        }

        return false;
    }

    public void setVal(int val) {
        this.val = val;
    }

    @Override
    public int compareTo(OrderElement order) {

        int gap = getVal() - order.getVal();

        return gap;
    }

    public static String createOrderString(Collection<OrderElement> orderSet) {

        String orderStr = "";

        if (!orderSet.isEmpty()) {

            OrderElement[] orders = orderSet.toArray(new OrderElement[1]);

            for (int index = 0; index < orders.length - 1; index++) {

                orderStr += orders[index].getVal() + ",";
            }

            orderStr += orders[orders.length - 1].getVal();
        }

        return orderStr;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}
